<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Document</title>
    <!-- 引入样式 -->
    <link href="../../plugins/element-ui/index.css" rel="stylesheet"/>
    <link href="../../styles/common.css" rel="stylesheet"/>
    <link href="../../styles/page.css" rel="stylesheet"/>
    <style>
        #member-app .notAdmin::after {
            border: 0 !important;

        }
    </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <div class="tableBar">
            <el-input
                    @keyup.enter.native="handleQuery"
                    clearable
                    placeholder="请输入账号"
                    style="width: 250px"
                    v-model="input"
            >
                <i
                        @click="handleQuery"
                        class="el-input__icon el-icon-search"
                        slot="prefix"
                        style="cursor: pointer"
                ></i>
            </el-input>
            <!--                    <el-button-->
            <!--                      type="primary"-->
            <!--                      @click="addMemberHandle('add')"-->
            <!--                    >-->
            <!--                      + 添加商家-->
            <!--                    </el-button>-->
        </div>
        <el-table
                :data="tableData"
                class="tableBox"
                stripe
        >
            <el-table-column
                    label="账号"
                    prop="username"
            ></el-table-column>
            <el-table-column
                    label="姓名"
                    prop="name"
            ></el-table-column>
            <el-table-column
                    label="手机号"
                    prop="phone"
            ></el-table-column>
            <el-table-column label="账号状态">
                <template slot-scope="scope">
                    {{ String(scope.row.status) === '0' ? '已禁用' : '正常' }}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作"
                    width="160"
            >
                <template slot-scope="scope">
                    <el-button
                            :class="{notAdmin:user !== 'admin'}"
                            @click="addMemberHandle(scope.row.id)"
                            class="blueBug"
                            size="small"
                            type="text"
                    >
                        编辑
                    </el-button>
                    <el-button
                            @click="statusHandle(scope.row)"
                            class="delBut non"
                            size="small"
                            type="text"
                            v-if="user === 'admin'"
                    >
                        {{ scope.row.status == '1' ? '禁用' : '启用' }}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page.sync="page"
                :page-size="pageSize"
                :page-sizes="[10, 20, 30, 40]"
                :total="counts"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                class="pageList"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/member.js"></script>
<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                id: '',
                status: '',
            }
        },
        computed: {},
        //钩子函数，初始化页面，设置用户名
        created() {
            this.init();
            this.user = JSON.parse(localStorage.getItem('userInfo')).username
        },
        mounted() {
        },
        methods: {
            //async异步
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined
                };
                //await等待
                await getMemberList(params).then(res => {
                    if (String(res.code) === '1') {
                        //code = 1，则请求成功，records是page对象的属性，即List<User>
                        this.tableData = res.data.records || [];
                        // counts总数
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            //查询处理器，重置page = 1，并加载
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加用户
            addMemberHandle(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '2',
                        url: '/backend/page/member/add.html',
                        name: '添加商家'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '2',
                        url: '/backend/page/member/add.html?id=' + st,
                        name: '修改员工'
                    }, true)
                }
            },
            //状态修改处理器
            statusHandle(row) {
                this.id = row.id;
                this.status = row.status;
                //确然调整状态弹窗
                this.$confirm('确认调整该账号的状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    //将status的值反转
                    enableOrDisableEmployee({'id': this.id, 'status': !this.status ? 1 : 0}).then(res => {
                        console.log('enableOrDisableEmployee', res);
                        if (String(res.code) === '1') {
                            this.$message.success('账号状态更改成功！');
                            this.handleQuery()
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init()
            }
        }
    })
</script>
</body>
</html>